package com.yuyou.fn.tradingservice.service.impl;

import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.tradingservice.entity.ServiceCartItem;
import com.yuyou.fn.tradingservice.entity.ServiceCategory;
import com.yuyou.fn.tradingservice.entity.ServiceProduct;
import com.yuyou.fn.tradingservice.service.IServiceCartItemService;
import com.yuyou.fn.tradingservice.service.IServiceCategoryService;
import org.springframework.stereotype.Service;

import com.yuyou.fn.tradingservice.mapper.ServiceCartMapper;
import com.yuyou.fn.tradingservice.entity.ServiceCart;
import com.yuyou.fn.tradingservice.service.IServiceCartService;
import com.github.wens.framework.service.impl.SuperServiceImpl;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 *
 * ServiceCart 表数据服务层接口实现类
 *
 */
@Service
public class ServiceCartServiceImpl extends SuperServiceImpl<ServiceCartMapper, ServiceCart,Long> implements IServiceCartService {

    @Resource
    private IServiceCartItemService cartItemService;

    @Resource
    private IServiceCategoryService categoryService;

    @Override
    public void addToCart(ServiceProduct product, Long userId, int quantity) {
        Example<ServiceCart> example = ServiceCart.createExample();
        example.createCriteria().andEqualTo("type",product.getType()).andEqualTo("userId", userId);
        ServiceCart serviceCart = this.findOneByExample(example);
        if(serviceCart == null) {
            serviceCart = new ServiceCart();
            serviceCart.setUserId(userId);
            if(product.getCategoryId() != null) {
                Long childrenId = product.getCategoryId();
                Long categoryId = findRootCategoryId(childrenId);
                serviceCart.setCategoryId(categoryId);
            }
            serviceCart.setCreateTime(TimeUtils.now());
            serviceCart.setUpdateTime(TimeUtils.now());
            serviceCart.setType(product.getType());
            insertSelective(serviceCart);
            cartItemService.addToCartItem(product, serviceCart.getCartId(), quantity);
        } else {
            //获取购物车条目中是否有productId，有就增加数量，没有就创建
            Example<ServiceCartItem> cartItemExample = ServiceCartItem.createExample();
            cartItemExample.createCriteria().andEqualTo("cartId",serviceCart.getCartId()).andEqualTo("productId", product.getProductId());
            ServiceCartItem cartItem = cartItemService.findOneByExample(cartItemExample);
            serviceCart.setUpdateTime(TimeUtils.now());
            if(cartItem == null) {
                cartItemService.addToCartItem(product, serviceCart.getCartId(), quantity);
            } else {
                BigDecimal p = cartItem.getPrice().divide(new BigDecimal(cartItem.getQuantity()));
                cartItem.setQuantity(cartItem.getQuantity()+quantity);
                BigDecimal q = new BigDecimal(cartItem.getQuantity());
                cartItem.setPrice(p.multiply(q));
                cartItem.setUpdateTime(TimeUtils.now());
                cartItemService.updateSelectiveById(cartItem);
            }
        }
    }

    @Override
    public void addRentToCart(ServiceProduct product, Long userId, String startDate, String endDate, String startTime, String endTime) {
        Example<ServiceCart> example = ServiceCart.createExample();
        example.createCriteria().andEqualTo("type",product.getType());
        example.createCriteria().andEqualTo("userId",userId);
        ServiceCart serviceCart = this.findOneByExample(example);
        if(serviceCart == null) {
            serviceCart = new ServiceCart();
            serviceCart.setUserId(userId);
            serviceCart.setCreateTime(TimeUtils.now());
            serviceCart.setUpdateTime(TimeUtils.now());
            serviceCart.setType(product.getType());
            insertSelective(serviceCart);
            cartItemService.addRentToCartItem(product, serviceCart.getCartId(), startDate,endDate,startTime,endTime);
        } else {
            serviceCart.setUpdateTime(TimeUtils.now());
            cartItemService.addRentToCartItem(product, serviceCart.getCartId(), startDate,endDate,startTime,endTime);
        }
    }

    public Long findRootCategoryId(Long categoryId) {
        ServiceCategory category = categoryService.findById(categoryId);
        Long parentCategoryId = category.getParentId();
        if(parentCategoryId != null) {
            findRootCategoryId(parentCategoryId);
            return parentCategoryId;
        } else {
            return categoryId ;
        }
    }
}